﻿Imports DataAccess
Imports DTO

Public Class BSTimetable

    Public Sub New(ByVal serverName As String)
        ' config server name
        DATimetable.ConfigServerName(serverName)
    End Sub

    Public Function GetTimetable() As DataTable
        ' Datatable get from database
        Return DATimetable.GetTimetable()
    End Function

    ''' <summary>
    ''' Get all timetable has not deleted
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetTimetable0() As DataTable
        ' Datatable get from database
        Return DATimetable.GetTimetable0()
    End Function

    Public Function GetRestTime(ByVal dtoTimetable As DTOTimetable) As DataTable
        Return DATimetable.GetRestTime(dtoTimetable)
    End Function

    ''' <summary>
    ''' Add new timetable
    ''' </summary>
    ''' <param name="dtoTimetable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function AddTimetable(ByVal dtoTimetable As DTOTimetable) As Boolean

        Dim result As Integer = DATimetable.AddTimetable(dtoTimetable)

        If result <> 0 Then

            For Each row As DataRow In dtoTimetable.DtRestTimeProperty.Rows

                result = DATimetable.AddRestTime(dtoTimetable.TimetableNoProperty, dtoTimetable.ExFromProperty,
                                                                 dtoTimetable.ExToProperty, row.Item("Rest From"), row.Item("Rest To"))
                If result = 0 Then

                    Return False

                End If
            Next

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Modify timetable
    ''' </summary>
    ''' <param name="dtoNTimetable">new timetable</param>
    ''' <param name="dtoOTimetable">old timetable</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ModifyTimetable(ByVal dtoNTimetable As DTOTimetable, ByVal dtoOTimetable As DTOTimetable,
                                    ByVal newExTo As String) As Boolean

        Dim result As Integer = DATimetable.DeleteTimetable(dtoOTimetable, newExTo)

        If result <> 0 Then

            If AddTimetable(dtoNTimetable) Then

                Return True

            Else

                Return False

            End If

        Else

            Return False

        End If

    End Function


    ''' <summary>
    ''' modify timetable in future is actually delete
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ModifyTimetableFuture(ByVal dtoNTimetable As DTOTimetable, ByVal dtoOTimetable As DTOTimetable) As Boolean

        If DATimetable.DeleteTimetableFuture(dtoOTimetable) Then

            If AddTimetable(dtoNTimetable) Then

                Return True

            Else

                Return False

            End If

        Else

            Return False

        End If

    End Function


    ''' <summary>
    ''' Delete timetable out of database
    ''' </summary>
    ''' <param name="dtoTimetable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteTimetableFuture(ByVal dtoTimetable As DTOTimetable) As Boolean

        If DATimetable.DeleteTimetableFuture(dtoTimetable) <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Delete Timetable
    ''' </summary>
    ''' <param name="dtoTimetable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteTimetable(ByVal dtoTimetable As DTOTimetable, ByVal newExTo As string) As Boolean

        If DATimetable.DeleteTimetable(dtoTimetable, newExTo) <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Delete Timetable had expired.
    ''' </summary>
    ''' <param name="dtoTimetable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteTimetableExpired(ByVal dtoTimetable As DTOTimetable) As Boolean

        If DATimetable.DeleteTimetableExpired(dtoTimetable) <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function


    ''' <summary>
    ''' check timetable if it's using
    ''' </summary>
    ''' <param name="dtoTimetable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CheckTimetableUsing(ByVal dtoTimetable As DTOTimetable) As Boolean

        Dim i As Integer = DATimetable.CheckTimetableUsing(dtoTimetable)

        If i <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Get exp from and exp to timetable 
    ''' </summary>
    ''' <param name="no"></param>
    ''' <param name="expFrom"></param>
    ''' <param name="expTo"></param>
    ''' <remarks></remarks>
    Public Sub GetExpTimetable(ByVal no As String, ByRef expFrom As String, ByRef expTo As String)

        Dim dt As DataTable = DATimetable.GetExpTimetable(no)

        expFrom = dt.Rows.Item(0).Item("EXPIRATION_FROM").ToString()

        expTo = dt.Rows.Item(0).Item("EXPIRATION_TO").ToString()

    End Sub

    ''' <summary>
    ''' Get Timetables is effecting
    ''' </summary>
    ''' <param name="startDate"></param>
    ''' <param name="endDate"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetTimetableEffect(ByVal startDate As String, ByVal endDate As String) As DataTable

        Return DATimetable.GetTimetableEffect(startDate, endDate)

    End Function


    ''' <summary>
    ''' get timetable infor by no, ex from, ex to
    ''' </summary>
    ''' <param name="timetableNo"></param>
    ''' <param name="exFrom"></param>
    ''' <param name="exTo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetTimetable(ByVal timetableNo As String, ByVal exFrom As String, ByVal exTo As String) As DataTable

        Return DATimetable.GetTimetable(timetableNo, exFrom, exTo)

    End Function

End Class
